<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\RoomPeople;
use app\common\model\ScoreLog;
use think\Collection;

class Score extends Api
{
    protected $noNeedLogin = [""];
    protected $noNeedRight = ["*"];

    /** 统计 */
    public function statistics()
    {
        $param = $this->request->param();
        $this->validateCommon("Score.statistics", $this->serial_number);
        $month            = $param['month'] ?? "";
        $type             = $param['type'] ?? "";
        $where['user_id'] = $this->auth->id;
        if (!empty($month)) {
            $startTime           = date('Y-m-d', strtotime("first day of {$month}"));
            $endTime             = date('Y-m-d', strtotime("{$month} +1 month"));
            $where['createtime'] = ['between', [$startTime, $endTime]];
        }
        if (!empty($type)) {
            if ($type == 1) {
                $where['is_win'] = 2;
            }
            if ($type == 2) {
                $where['is_win'] = 3;
            }
        } else {
            $where['is_win'] = ['in', [2, 3]];
        }
        $list = RoomPeople::where($where)->group('room_id')->field('status,is_win isWin,createtime createTime')->select();
        // 将数组中 isWin 字段的值作为键，出现的次数作为值统计到一个新数组中
        $counts = array_count_values(array_column($list, 'isWin'));
        // 获取 isWin 值为 2 和 3 的数量
        $victoryCount = $counts['2'] ?? 0;
        $failCount    = $counts['3'] ?? 0;
        $arr          = [
            'victoryCount' => $victoryCount,
            'failCount'    => $failCount,
            'item'         => $list,
        ];
        $this->success('', $arr);
    }

    /** 排行榜 */
    public function rankingList()
    {
        $param = $this->request->param();
        $this->validateCommon("Score.rankingList", $this->serial_number);
        $type    = $param['timeType'];
        $agoTime = time();
        if ($type == 1) {
            // 获取一周前的时间戳（即当前时间往前推7天）
            $agoTime = strtotime("-1 week", time());
        }
        if ($type == 2) {
            // 获取一月前的时间戳（即当前时间往前推7天）
            $agoTime = strtotime("-1 month", time());
        }
        $nowDate = date("Y-m-d H:i:s", time());
        // 将时间戳格式化为日期字符串（如果需要）
        $agoDate             = date("Y-m-d H:i:s", $agoTime);
        $where['createtime'] = ['between', [$agoDate, $nowDate]];
        $item                = ScoreLog::where($where)
                                       ->field('after,user_id userId')
                                       ->select();
        $newItem             = [];
        foreach ($item as $value) {
            $userInfo  = \app\common\model\User::where('id', $value['userId'])->field('avatar,username,level')->find();
            $newItem[] = ['after' => $value['after'], 'userId' => $value['userId'], 'userName' => $userInfo['username'], 'avatar' => getCurrentUrl($userInfo['avatar']), 'level' => $userInfo['level']];
        }
        // 提取排序所需字段
        $after = array_column($newItem, 'after');
// 按照 'after' 字段的值进行排序
        array_multisort($after, SORT_DESC, $newItem);
// 添加名次字段
        $rank = 1;
        foreach ($newItem as &$entry) {
            $entry['rank'] = $rank++;
        }
        $this->success('', $newItem);
    }
}